﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CMN.Models;
using CMN.Entity;
using CMN.Config;
using CMN.Common;
using CMN.Models.Interfaces;
using System.Web;
using CMN.BLL;
using System.Threading;

namespace CMN.Forms
{
    public partial class frmSearchUrlLog : Form
    {
         private AsyncUpdateUrlLog m_AsyncImport;


         private  int siteId = 0;


         public frmSearchUrlLog()
        {
            InitializeComponent();

            dateEnd.Text = DateTime.Now.AddDays(1).ToString();
        }
         public frmSearchUrlLog(int siteId)
         {
             InitializeComponent();
             this.siteId = siteId;
             if (siteId != 0)
             {
                 QueryParam qp = new QueryParam();

                 qp.Where = " SiteId=" + siteId.ToString();
                 bingGridUrlLog(qp);
             }
             dateEnd.Text = DateTime.Now.AddDays(1).ToString();
         }


         private void bingGridUrlLog(QueryParam qp)
         {
             gridurlLog.Rows.Clear();
             SendUrlLogEntity urlLog = new SendUrlLogEntity();
             IDataService<SendUrlLogEntity> urlLogSv = ServiceBuilder.BuildSendUrlLogService();
             int recordNum = 0;
             List<SendUrlLogEntity> urlLogList = urlLogSv.List(qp, out recordNum);
             int index = 0;
             DataGridViewRow Row = null;

             foreach (SendUrlLogEntity info in urlLogList)
             {
                 index = this.gridurlLog.Rows.Add();
                 Row = this.gridurlLog.Rows[index];
                 Row.Cells["ID"].Value = info.Id.ToString();
                 Row.Cells["AddDate"].Value = info.AddDate.ToString();
                 Row.Cells["IsIndex"].Value = info.IsIndex;

                 Row.Cells["SendLoginName"].Value = info.SendLoginName;
                 Row.Cells["SendUrl"].Value = info.SendUrl;
                 Row.Cells["SEOKeyWord"].Value = info.SEOKeyWord;
                 Row.Cells["SEOSiteUrl"].Value = info.SEOSiteUrl;
                 Row.Cells["SEOUrl"].Value = info.SEOUrl;

                 Row.Cells["site_Id"].Value = info.SiteId.ToString();

             }
             DataGridViewTextBoxColumn dgv_Text = new DataGridViewTextBoxColumn();
             for (int i = 0; i < gridurlLog.Rows.Count; i++)
             {
                 int j = i + 1;
                 gridurlLog.Rows[i].HeaderCell.Value = j.ToString();
             }

         }

        private void gridurlLog_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //Clipboard.SetText(gridurlLog[e.ColumnIndex, e.RowIndex].Value.ToString());
        }

        private void gridurlLog_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 2 || e.ColumnIndex == 3 || e.ColumnIndex == 4)
            {
                System.Diagnostics.Process.Start("chrome.exe", this.gridurlLog.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
            }
        }

       

        private void btnUpdateIndex_Click(object sender, EventArgs e)
        {

            
            this.m_AsyncImport = new AsyncUpdateUrlLog(this);
            this.m_AsyncImport.TaskProgress += new EventHandler(AsyncImport_TaskProgress);
            this.m_AsyncImport.TaskDone += new EventHandler(AsyncImport_TaskDone);
            m_AsyncImport.StrWhere = getWhere()+" and isIndex!='是' ";
            m_AsyncImport.Start();
            btnUpdateIndex.Enabled = false;

        }
        void AsyncImport_TaskDone(object sender, EventArgs e)
        {
            btnUpdateIndex.Enabled = true;
            lbUpdate.Text = "完成";
        }
        void AsyncImport_TaskProgress(object sender, EventArgs e)
        {
            AsyncEventArgs pAsyncEventArgs = e as AsyncEventArgs;
            int step = (int)pAsyncEventArgs.getObjects[0];
            lbUpdate.Text = "共" + pAsyncEventArgs.action.ToString() + "当前第" + (step + 1).ToString();

        }
        private void btnSearch_Click(object sender, EventArgs e)
        {
           
            QueryParam qp = new QueryParam();
           
            qp.Where = getWhere();
            bingGridUrlLog(qp);
        }
        private string getWhere()
        {
            BLLHelper bllHelper = new BLLHelper();
            string strDateStart = bllHelper.GetSqlDateTime(Utils.StringToDataTime(dateStart.Text));
            string strDateEnd = bllHelper.GetSqlDateTime(Utils.StringToDataTime(dateEnd.Text));
            string strWhere = string.Concat("  adddate >=", strDateStart, " and adddate<=", strDateEnd, " ");
            
            //qp.ReturnFields = "Id,urlLogName,urlLogUrl,PR,BR,IndexNum,SendUrlType,LoginName,Mark,(select count(1) from SendUrlLog where urlLogId=urlLogInfo.id and " + strWhere + " ) as UrlCount,(select count(1) from SendUrlLog where urlLogId=urlLogInfo.id and isindex='是'  and " + strWhere + " ) as UrlIndexCount  ";
            if (tbSendUrl.Text != "")
                strWhere = string.Concat(strWhere, " and SendUrl like '%", tbSendUrl.Text, "%' ");
            if (tbSeoUrl.Text != "")
                strWhere = string.Concat(strWhere, " and SEOUrl like '%", tbSeoUrl.Text, "%' ", " or SEOSiteUrl like '%", tbSeoUrl.Text, "%' ");
            if (tbSendUserName.Text != "")
                strWhere = string.Concat(strWhere, " and SendLoginName like '", tbSendUserName.Text, "%' ");
            if (tbSeoKeyWord.Text != "")
                strWhere = string.Concat(strWhere, " and SEOKeyWord like '%", tbSeoKeyWord.Text, "%' ");
           

            if (tbIsIndex.Text != "")
                strWhere = string.Concat(strWhere, " and  isIndex ='", tbSeoKeyWord.Text, "' ");
            return strWhere;
        }
        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源，为 true；否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            //停止工作线程
            if( this.m_AsyncImport!=null)
            this.m_AsyncImport.Stop();
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        

        private void gridurlLog_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1 && this.gridurlLog.Rows.Count > e.RowIndex)
            {

                frmAddUrlLog frmUrl = new frmAddUrlLog(Utils.StringToInt(this.gridurlLog.Rows[e.RowIndex].Cells[0].Value.ToString(), 0));

                frmUrl.ShowDialog();
            }
        }

        private void gridurlLog_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
         
           
            
        
        }

       

      
    }
}
